home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 February: Tool Chest / Dev.CD Feb 99 TC.toast / Tool Chest / Interapplication Communication / ScriptableStuffItEngine / Project / SSE_MakeSegments.cp < prev    next >
Encoding:
Text File  |  1998-12-02  |  4.7 KB  |  201 lines  |  [TEXT/CWIE]

  1. /*
  2.     You may incorporate this Apple sample source code into your program(s) without
  3.     restriction. This Apple sample source code has been provided "AS IS" and the
  4.     responsibility for its operation is yours. You are not permitted to redistribute
  5.     this Apple sample source code as "Apple sample source code" after having made
  6.     changes. If you're going to re-distribute the source, we require that you make
  7.     it clear in the source that the code was descended from Apple sample source
  8.     code, but that you've made changes.
  9. */
  10.  
  11. #include "ScriptableStuffItEngine.h"
  12.  
  13. #ifndef __SCRIPT__
  14. #    include <Script.h>
  15. #endif
  16.  
  17. #ifndef __PLSTRINGFUNCS__
  18. #    include <PLStringFuncs.h>
  19. #endif
  20.  
  21. #ifndef __StuffItLib__
  22. #    include "StuffItEngineLib.h"
  23. #endif
  24.  
  25. #ifndef __ERRORS__
  26. #    include <Errors.h>
  27. #endif
  28.  
  29. static pascal OSErr GetOptionalFilenameTemplate (const AppleEvent *ae, Str31 nameTemplate)
  30. {
  31.     OSErr        err                = noErr;
  32.     DescType    actualType;
  33.     Size        actualSize;
  34.  
  35.     err = AESizeOfParam (ae,keyNameTemplate,&actualType,&actualSize);
  36.  
  37.     if (err == errAEDescNotFound)
  38.     {
  39.         err = noErr;
  40.         *nameTemplate = 0;
  41.     }
  42.     else if (actualType != typeChar)
  43.         err = paramErr;
  44.     else
  45.     {
  46.         Size largestLegalSizeForNameTemplate = sizeof (Str31) - 1;
  47.  
  48.         if (actualSize > largestLegalSizeForNameTemplate)
  49.             err = paramErr;
  50.         else if (!(err = AEGetParamPtr (ae,keyNameTemplate,typeChar,
  51.             &actualType,nameTemplate+1,largestLegalSizeForNameTemplate,&actualSize)))
  52.         {
  53.             *nameTemplate = actualSize;
  54.         }
  55.     }
  56.  
  57.     return err;
  58. }
  59.  
  60. static pascal OSErr FSpIsDir (const FSSpec *spec, Boolean *isDir)
  61. {
  62.     OSErr err = noErr;
  63.  
  64.     CInfoPBPtr pbp = (CInfoPBPtr) NewPtrClear (sizeof (*pbp));
  65.  
  66.     if (!pbp)
  67.         err = MemError ( );
  68.     else
  69.     {
  70.         pbp->dirInfo.ioVRefNum = spec->vRefNum;
  71.         pbp->dirInfo.ioDrDirID = spec->parID;
  72.         pbp->dirInfo.ioNamePtr = (StringPtr) spec->name;
  73.  
  74.         err = PBGetCatInfoSync (pbp);
  75.  
  76.         if (!err)
  77.             *isDir = (pbp->hFileInfo.ioFlAttrib & ioDirMask) != 0;
  78.  
  79.         DisposePtr ((Ptr) pbp);
  80.     }
  81.  
  82.     return err;
  83. }
  84.  
  85. static pascal OSErr GetDestination (const AppleEvent *ae, const FSSpec *sourceFSS, FSSpecPtr destFSS)
  86. {
  87.     OSErr err = noErr;
  88.  
  89.     Size        actualSize;
  90.     DescType    actualType;
  91.  
  92.     if (!(err = AEGetParamPtr (ae,keyDestination,typeFSS,&actualType,destFSS,sizeof(*destFSS),&actualSize)))
  93.     {
  94.         if (typeFSS != actualType)
  95.             err = paramErr;
  96.         else
  97.         {
  98.             Boolean isDir;
  99.  
  100.             if (!(err = FSpIsDir (destFSS,&isDir)) && isDir)
  101.             {
  102.                 //
  103.                 //    gotta provide a filename even though the engine will not use it
  104.                 //
  105.  
  106.                 Str63 buf = "\p:";
  107.                 PLstrcat (buf,destFSS->name);
  108.                 PLstrcat (buf,"\p:segment");
  109.  
  110.                 err = FSMakeFSSpec (destFSS->vRefNum,destFSS->parID,buf,destFSS);
  111.                 if (err == fnfErr) err = noErr;
  112.             }
  113.         }
  114.     }
  115.     else if (err == errAEDescNotFound)
  116.     {
  117.         err = FSMakeFSSpec (sourceFSS->vRefNum,sourceFSS->parID,sourceFSS->name,destFSS);
  118.     }
  119.  
  120.     return err;
  121. }
  122.  
  123. static pascal OSErr SpoofSourceFileName (const AppleEvent *ae, FSSpecPtr fssP, Str31 oldFileName)
  124. {
  125.     //
  126.     //    This function exists only because StuffIt Engine does not listen when
  127.     //    you tell it what segment name to use; it always derives the segment name
  128.     //    from the source file name. So we change the source file name. It's up
  129.     //    to the caller of this function to change it back.
  130.     //
  131.  
  132.     OSErr err = noErr;
  133.  
  134.     if (*(fssP->name) > 31)
  135.         err = paramErr;
  136.     else
  137.     {
  138.         Str31 filenameTemplate;
  139.  
  140.         if (!(err = GetOptionalFilenameTemplate (ae,filenameTemplate)) && *filenameTemplate)
  141.         {
  142.             if (!(err = FSpRename (fssP,filenameTemplate)))
  143.             {
  144.                 PLstrcpy (oldFileName,fssP->name);
  145.                 PLstrcpy (fssP->name,filenameTemplate);
  146.             }
  147.         }
  148.     }
  149.  
  150.     return err;
  151. }
  152.  
  153. pascal OSErr MakeSegments (const AppleEvent *ae, AppleEvent *reply, long magicCookie)
  154. {
  155.     AEDesc fssDesc;
  156.  
  157.     OSErr err = AEGetParamDesc (ae,keyDirectObject,typeFSS,&fssDesc);
  158.  
  159.     if (!err)
  160.     {
  161.         long        segSize;
  162.         DescType    actualType;
  163.         Size        actualSize;
  164.  
  165.         if (!(err = AEGetParamPtr
  166.             (ae,keySegmentSize,typeLongInteger,&actualType,&segSize,sizeof(segSize),&actualSize)))
  167.         {
  168.             Boolean selfExtracting = false;
  169.  
  170.             if (!(err = GetOptionalBoolean (ae,&selfExtracting,keySegsAreSelfExtracting)))
  171.             {
  172.                 HLockHi (fssDesc.dataHandle);
  173.                 FSSpecPtr sourceFSS = FSSpecPtr (*(fssDesc.dataHandle));
  174.  
  175.                 FSSpec    destFSS;
  176.                 short    numSegments;
  177.                 Str31    oldName;
  178.  
  179.                 if (!(err = CountSegments (magicCookie,sourceFSS,segSize,&numSegments)))
  180.                 if (!(err = GetDestination (ae,sourceFSS,&destFSS)))
  181.                 if (!(err = SpoofSourceFileName (ae,sourceFSS,oldName)))
  182.                 {
  183.                     if (!(err = SegmentFSSpec (magicCookie,sourceFSS,&destFSS,segSize,selfExtracting,false)))
  184.                         err = AEPutParamPtr (reply,keyDirectObject,typeShortInteger,&numSegments,sizeof(numSegments));
  185.  
  186.                     if (PLstrcmp (sourceFSS->name,oldName))
  187.                     {
  188.                         OSErr err2 = FSpRename (sourceFSS,oldName);
  189.                         if (!err) err = err2;
  190.                     }
  191.                 }
  192.             }
  193.         }
  194.  
  195.         OSErr err2 = AEDisposeDesc (&fssDesc);
  196.         if (!err) err = err2;
  197.     }
  198.  
  199.     return err;
  200. }
  201.